package com.itheima.info.manage.controller;

import com.itheima.info.manage.domain.Student;
import com.itheima.info.manage.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Yao
 * @date 2020/6/20 0020
 **/
public class StudentController {

    private  StudentService studentService=new StudentService();

    //优化
    private Scanner sc = new Scanner(System.in);

    public  void start() {

        Scanner sc = new Scanner(System.in);

        while (true) {
            // 1. 搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    // System.out.println("查看学生");
                    queryStudents();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public void queryStudents() {

        ArrayList<Student> list = studentService.queryStudents();

        if (list==null){
            System.out.println("查无信息，请添加!");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t" + stu.getAge()+ "\t\t" + stu.getBirthday());
        }


    }

    public void updateStudent() {

        String sid;
        while (true){
            System.out.println("请输入学生号：");
            sid = sc.next();
            int index = studentService.getIndex(sid);
            if (index==-1){
                System.out.println("学生号不存在！请重新输入");
            }else {
                break;
            }
        };
        Student stu = inputStudent(sid);
        studentService.updateStudent(stu);

        System.out.println("修改成功！");
    }

    public void deleteStudent() {

        int index;
        while (true){
            System.out.println("请输入学生号：");
            String sid = sc.next();
            index = studentService.getIndex(sid);
            if (index==-1){
                System.out.println("学生号不存在！请重新输入");
            }else {
                break;
            }
        }


        studentService.deleteStudent(index);

        System.out.println("删除成功！");
    }


    public void addStudent() {

        String sid;
        while (true) {
            System.out.println("请输入学生号：");
            sid = sc.next();
            int index = studentService.getIndex(sid);
            if (index == -1) {
                break;
            } else {
                System.out.println("学生号已存在！请重新输入");
            }
        }
        Student stu = inputStudent(sid);
        studentService.addStudent(stu);
        System.out.println("添加成功");

    }



    public Student inputStudent(String sid) {

        System.out.println("请输入学生姓名：");
        String name = sc.next();

        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();

        System.out.println("请输入学生生日：");
        String birth = sc.next();

        Student stu = new Student(sid, name, age, birth);
        return stu;
    }
}
